package org.boot.fast.dao;

import org.boot.fast.pojo.ExcelConfigDO;
import org.boot.fast.pojo.QueryCondition;
import org.boot.fast.repository.JdbcRepositoryUtil;
import org.boot.fast.util.ConverterUtil;
import org.boot.fast.util.MappingUtil;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Component
public class ExcelConfigDAO {

    private static final String TABLE = "fast_excel_config";

    @Transactional(rollbackFor = Exception.class)
    public Long save(ExcelConfigDO excelConfigDO) {
        Map<String, Object> dataMap = MappingUtil.mappingToTable(ConverterUtil.convertToMap(excelConfigDO));
        return JdbcRepositoryUtil.insertBack(TABLE, dataMap);
    }

    @Transactional(rollbackFor = Exception.class)
    public int update(ExcelConfigDO fileInfoDO) {
        Map<String, Object> dataMap = MappingUtil.mappingToTable(ConverterUtil.convertToMap(fileInfoDO));
        List<QueryCondition> conditions = new ArrayList<>();
        conditions.add(new QueryCondition("id", "=", fileInfoDO.getId()));
        return JdbcRepositoryUtil.update(TABLE, dataMap, conditions);
    }

    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id) {
        List<QueryCondition> conditions = new ArrayList<>();
        conditions.add(new QueryCondition("id", "=", id));
        return JdbcRepositoryUtil.delete(TABLE, conditions);
    }

    public ExcelConfigDO findOne(Long id) {
        return ConverterUtil.convert(
                MappingUtil.mappingToJava(
                        JdbcRepositoryUtil.findById(TABLE, id)
                ), ExcelConfigDO.class);
    }

    public ExcelConfigDO findByKey(String configKey) {
        List<QueryCondition> conditions = new ArrayList<>();
        conditions.add(new QueryCondition("config_key", "=", configKey));
        return ConverterUtil.convert(
                MappingUtil.mappingToJava(
                        JdbcRepositoryUtil.findOne(TABLE, conditions)
                ), ExcelConfigDO.class);
    }

    public List<ExcelConfigDO> findList(ExcelConfigDO excelConfigDO) {
        List<QueryCondition> conditions = new ArrayList<>();
        if(excelConfigDO != null){
            if (excelConfigDO.getId() != null) conditions.add(new QueryCondition("id", "=", excelConfigDO.getId()));
            if (excelConfigDO.getConfigKey() != null) conditions.add(new QueryCondition("config_key", "=", excelConfigDO.getConfigKey()));
            if (excelConfigDO.getTag() != null) conditions.add(new QueryCondition("tag", "=", excelConfigDO.getTag()));
            if (excelConfigDO.getTableName() != null) conditions.add(new QueryCondition("table_name", "=", excelConfigDO.getTableName()));
        }
        return ConverterUtil.convert(
                MappingUtil.mappingToJava(
                        JdbcRepositoryUtil.find(TABLE, conditions)
                ), ExcelConfigDO.class);
    }

}